<template>
	<view class="main-box" />
</template>
<script setup name='index'>
	import {
		reactive,
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app'
	import {
		getPlatformCode
	} from '@/common/platformApi.js'
	import {
		urlToObj
	} from '@/common/common.js'
	import {
		useCommonStore
	} from '@/stores/common.js'
	const commonStore = useCommonStore()

	const {
		proxy
	} = getCurrentInstance()

	const getOpenidData = ref({})

	// web参数
	const wabData = ref({})
	// 支付方式
	const payWay = ref('')
	// 通道列表
	const payMethod = ref([])
	// 选择的通道
	const payWayObj = ref({})
	// 用户code
	const code = ref('')
	// 商户信息
	const merchantInfo = ref()

	onLoad(async (options) => {
		console.log('获取options===>', JSON.stringify(options));
		getEwmData(options)
		getWebData(options)
		// 获取通道列表
		payMethod.value = await commonStore.getChannelList()

		// 获取商家信息
		merchantInfo.value = await commonStore.getMerchantInfo()

		if (proxy.$System.getEnv() == 'h5') {
			proxy.$System.goTo('/pages/register/index', wabData.value)
		}
		code.value = options?.code || await getPlatformCode()
		await payInit()
	})
	// 默认操作
	const payInit = async () => {
		// 获取默认PayWay
		payWay.value = commonStore.getPayWay()

		// 默认选中通道
		payWayObj.value = payMethod.value.filter(e => e.payWay == payWay.value)[0]
		mpPayWay()

		console.log('默认选中通道==>', payWayObj.value);
		getOpenidData.value = {
			...payWayObj.value,
			...merchantInfo.value,
			code: code.value,
		}
		console.log('获取openId参数==>', getOpenidData.value);

		await commonStore.getOpenId(getOpenidData.value)

		proxy.$System.goTo('/pages/register/index', wabData.value)
	}
	// 扫二维码进来获取二维码参数
	const getEwmData = (data) => {
		const app = getApp();
		console.log('app===>', app);
		console.log('options参数===>', data);
		let deviceData = !commonStore.deviceInfo?.deviceNo ? {
			deviceNo: 'ZZB000000001754967350569', //'ZZB000000001753266877372',
			deviceType: '0',
			// tradeAmt: 1, //一分
			// mId: 'T20250816070433749556',
		} : commonStore.deviceInfo

		if (data?.q) {
			const url = decodeURIComponent(data.q)
			deviceData = urlToObj(url)
		}
		if (app.globalData.qrParams?.qrCode) {
			// 支付宝获取
			deviceData = urlToObj(app.globalData.qrParams.qrCode)
		}
		if (data.deviceNo && data.deviceType) {
			deviceData = {
				...data
			}
		}
		console.log('码牌信息==>', deviceData);
		// 保存码牌信息
		commonStore.setDeviceInfo(deviceData)
		return deviceData
	}
	// 浏览器进来的获取浏览器带进来的参数
	const getWebData = (data) => {
		if (!data.payVal) return
		wabData.value = data
	}

	// 判断对应小程序是否有通道
	const mpPayWay = () => {
		// #ifdef MP-ALIPAY||MP-WEIXIN
		if (!payWayObj.value?.payWay) {
			let content = ''
			// #ifdef MP-ALIPAY
			content = '当前商户未开通支付宝通道！'
			// #endif

			// #ifdef MP-WEIXIN
			content = '当前商户未开通微信通道！'
			// #endif
			uni.showModal({
				content: content,
				showCancel: false,
				success: (res) => {
					if (res.confirm) {
						mpPayWay()
					}
				}
			})
		}
		// #endif
	}
</script>
<style lang='scss' scoped>
</style>